<?php defined('SYSPATH') or die('No direct script access.');
require ('application/classes/dao/base.php');

class Controller_act extends Controller {
	/**
	 * @var  Request  Request that created the controller
	 */
	public $request;

	/**
	 * @var  Response The response that will be returned from controller
	 */
	public $response;


		
	

	public   function action_list()
	{

		try{
			$parm = post($this->request);
			if(!isset($parm['row']) ||!is_numeric($parm['row'])){
				$parm ['row'] = 11;
			}
			if(!isset($parm['start']) ||!is_numeric($parm['start'])){

				$parm ['start'] = 1;
			}
			$sql = 'select * from sysactivity inner join sysDictionary on (sysactivity.Dictionary=sysDictionary.id) inner join sysuser_detail on (sysuser_detail.userid=sysactivity.userid) ';
			if(isset($parm['id']) &&$parm['id']!="-1"){
				$sql = $sql.'where sysactivity.Dictionary ='.$parm['id'];
			}else{
				$sql =$sql. 'where 1=1';
			}
			if(isset($parm['category']) &&$parm['category']!=""){
				$sql = $sql.' and sysactivity.category ="'.$parm['category'].'"';
			}else{
				$sql = $sql.' and  1=1';
			}
			$sql = $sql.' order by sysactivity.createDate desc limit  '.($parm['start']-1)*$parm['row'].','.$parm['row'];

	  		$query = DB::query(Database::SELECT,$sql )->
	        execute()->as_array();
			
		}catch(Exception $e){

			echo json_encode(error($e->getMessage()));
			exit;
		}
		$this->response->body( json_encode(success($query)));
	}
	public   function action_detail(){
		try{
			$parm = get($this->request);
			
			if(!isset($parm['id']) ||$parm['id']==""){

				throw new Exception("分类不能为空");
			}


	  		$query = DB::select('*')->from('sysactivity')
              ->where("sysactivity.id","=",$parm['id'])->execute()->as_array();
	
	
		}catch(Exception $e){

			echo json_encode(error($e->getMessage()));
			exit;
		}
		$this->response->body( json_encode(success($query)));
	}


	 public function action_save(){


        try{


            $user = checkSession(Session::instance());

            if($user == null) {
                throw new Exception("没有登陆");
            }
            $parm = get($this->request);

            if(!isset($parm['id'])||$parm['id'] == "" ){
                throw new Exception("帖子不存在，尝试刷新");
            }
            if(!isset($parm['content'])||$parm['content'] == "" ){
                throw new Exception("内容不能为空");
            }
            Database::instance()->begin();

            DB::insert("sysreply_act",array('newid','content','userId','parentId','user'))
                ->values(array($parm['id'],$parm['content'],$user['id'],$parm['parentId'],$user['user']))->execute();
            $query = DB::query(Database::UPDATE, 'update sysnews set replying=replying+1 where  id='.$parm['id'])->execute();

            Database::instance()->commit();
            echo json_encode(success("发布成功"));
        }catch(Exception $e){
            Database::instance()->rollback();
            echo json_encode(error($e->getMessage()));
        }
    }
    public function action_reply()
    {

    	$parm = get($this->request);
        $query = "";
       try{
           if (!isset($parm['id']) || $parm['id'] == "") {
                throw new Exception("帖子不存在，尝试刷新");
            }

           $query = DB::query(Database::SELECT, 'SELECT sysreply_act.id,sysreply_act.parentId, sysreply_act.`user` ,sysuser_detail.photo,sysreply_act.content,sysreply_act.createDate,sysuser_detail.realname,reply.content as reply_content,reply.createDate as reply_createDate,reply.`user` as reply_user,replyuser.realname reply_realname,replyuser.photo as reply_photo FROM sysreply_act LEFT JOIN sysreply_act AS reply ON (reply.parentId = sysreply_act.id)left JOIN sysuser_detail replyuser ON (replyuser.`user` = reply.`user`) INNER JOIN sysuser_detail ON (sysuser_detail.`user` = sysreply_act.`user`)  where  sysreply_act.newid='.$parm['id'].'  group by sysreply_act.id  order by id  limit '.$parm['start'].' ,'.$parm['row'])->execute()->as_array();

       }catch(Exception $e){
            echo json_encode(error($e->getMessage()));
           exit;
       }
        echo json_encode(success($query));
    }
    public function action_join(){
    	  try{
    	  	$user = checkSession(Session::instance());
            if($user == null) {
                throw new Exception("没有登陆");
            }

    	  	$parm = get($this->request);

    	  	$parm['user'] = $user['user'];
    	  	$parm['userId'] = $user['id'];
    		$msg = rpcCall('actService.joinAct',$parm);
    	   }catch(Exception $e){
            echo json_encode(error($e->getMessage()));
           exit;
       }
   		 echo json_encode(success($msg));
    }
}